package com.zhf.eduuserboot.controller;

import com.zhf.eduuserboot.entity.User;
import com.zhf.eduuserboot.entity.UserDTO;
import com.zhf.eduuserboot.entity.UserVo;
import com.zhf.eduuserboot.service.UserService;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.File;
import java.util.UUID;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2021/2/14 13:43
 */
@RestController
@RequestMapping("user")
@CrossOrigin //跨域
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("login")
    public UserDTO login(String phone, String password, String nickName, String headImg){
        User user=null;
        UserDTO userDTO = new UserDTO();
        System.out.println("phone: " + phone);
        System.out.println("password: " + password);
        //检测手机号是否注册
        Integer i = userService.checkPhone(phone);
        if (i==0){
            userService.register(phone, password,nickName,headImg);
            userDTO.setMessage("手机号尚未注册，系统已帮您自动注册，请牢记密码！");
            user = userService.login(phone, password);
            userDTO.setState(200);
        }else{
            user = userService.login(phone, password);
            if (user != null) {
                userDTO.setState(200);
                userDTO.setMessage("登录成功！");
            }else{
                userDTO.setState(300);
                userDTO.setMessage("登录失败！");
            }
        }
        userDTO.setContent(user);
        return userDTO;
    }

    @PostMapping("/upload")
    public UserDTO upload(MultipartHttpServletRequest request) throws Exception {

        //从请求中获取上传的文件对象
        MultipartFile file = request.getFile("fname");

        //从文件对象中获取文件的原始名称
        String oldFilename = file.getOriginalFilename();

        //通过字符串截取的方式，从文件原始名中获取文件的后缀
        String endName = oldFilename.substring(oldFilename.lastIndexOf(".") + 1);

        //为了避免文件因为同名而覆盖，生成全新的文件名
        String newFileName = UUID.randomUUID().toString() + "." + endName;

        //创建web服务器保存文件的目录
        File toSaveFile = new File("D:/upload/" + newFileName);

        //将路径转换成文件
        file.transferTo(toSaveFile);

        //获取服务器的绝对路径
        String newFilePath = toSaveFile.getAbsolutePath();

        /*
        ----------------------------------------------------------------
         */
        //把文件从web服务器上传到fastDFS
        ClientGlobal.initByProperties("config/fastdfs-client.properties");
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        StorageServer storageServer = null;
        StorageClient1 client1 = new StorageClient1(trackerServer, storageServer);
        NameValuePair[] list = new NameValuePair[1];
        list[0] = new NameValuePair("fileName", oldFilename);
        String fileID = client1.upload_file1(newFilePath, endName, list);
        trackerServer.close();

        //封装fileSystem对象
        UserDTO userDTO = new UserDTO();
        userDTO.setState(200);
        userDTO.setMessage("头像上传成功！");
        userDTO.setContent("http://192.168.11.128/"+fileID);
        return userDTO;
    }

    @PostMapping("/updateUserInfo")
    public UserDTO updateUserInfo(@RequestBody UserVo userVo){
        UserDTO userDTO = new UserDTO();
        if (userService.updatePortraitAndName(userVo.getUserid(),userVo.getPortrait(),userVo.getName())==1) {
            userDTO.setState(200);
            userDTO.setMessage("修改成功！");
            userDTO.setContent(true);
        }else{
            userDTO.setState(500);
            userDTO.setMessage("修改失败！");
            userDTO.setContent(false);
        }
        return userDTO;
    }

    @PostMapping("/updatePassword")
    public UserDTO updatePassword(@RequestBody UserVo userVo){
        UserDTO userDTO = new UserDTO();
        if (userService.updatePassword(userVo.getUserid(),userVo.getPassword())==1) {
            userDTO.setState(200);
            userDTO.setMessage("修改成功！");
            userDTO.setContent(true);
        }else{
            userDTO.setState(500);
            userDTO.setMessage("修改失败！");
            userDTO.setContent(false);
        }
        return userDTO;
    }
}
